package com.colorful.insuranceproject.mine;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.colorful.insuranceproject.R;
import com.colorful.insuranceproject.base.TitleActivity;
import com.colorful.insuranceproject.consts.CodeTable;
import com.colorful.insuranceproject.request.MicroDrugRequest;
import com.github.yoojia.zxing.qrcode.Encoder;
import com.lovely3x.common.utils.Response;
import com.lovely3x.common.utils.ViewUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by handsome-Bob on 2017/9/13.
 * <p>
 * 药店退款
 */
public class CollectionCodeActivity extends TitleActivity implements TextWatcher {

    private static final int SET_RETURN_MONEY_URL = 0X1;

    private MicroDrugRequest microDrugRequest;

    @Bind(R.id.tv_show_money)
    TextView tvShowMoney;

    @Bind(R.id.ll_layout)
    LinearLayout llLayout;

    @Bind(R.id.et_money)
    EditText etMoney;

    @Bind(R.id.tv_btn)
    TextView tvBtn;

    @Bind(R.id.view_qr)
    ImageView qr;

    Bitmap mQrCodeImage;


    @Override
    protected int getContentView() {
        return R.layout.activity_collection_code;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setTitle(getString(R.string.durg_collection));
        getViewDivider().setVisibility(View.GONE);
    }

    @Override
    protected void onViewInitialized() {
        etMoney.addTextChangedListener(this);
        tvBtn.setClickable(false);
        microDrugRequest = new MicroDrugRequest(getHandler());

        String qrStr = "mount_not_set";
        initQr(qrStr);
    }

    private void initQr(String qrStr) {
          /*
         *   这里我需要加载一个二维码
         */
        int dimension = ViewUtils.dp2pxF(200);
        dimension = Math.min(ViewUtils.getWidth(), dimension);

        final Encoder encoder = new Encoder.Builder()
                .setBackgroundColor(0xFFFFFFFF) // 指定背景颜色，默认为白色
                .setCodeColor(0xFF000000) // 指定编码块颜色，默认为黑色
                .setOutputBitmapWidth(dimension) // 生成图片宽度
                .setOutputBitmapHeight(dimension) // 生成图片高度
                .setOutputBitmapPadding(0) // 设置为没有白边
                .build();

        this.mQrCodeImage = encoder.encode(qrStr);
        qr.setImageBitmap(mQrCodeImage);
    }

    @Override
    public void restoreInstanceOnCreateBefore(@NonNull Bundle savedInstance) {

    }

    @Override
    public void restoreInstanceOnCreateAfter(@NonNull Bundle savedInstance) {

    }

    @Override
    protected void handleResponseMessage(Message msg, Response response) {
        super.handleResponseMessage(msg, response);
        switch (msg.what) {
            case SET_RETURN_MONEY_URL:
                dismissProgressCircle();
                if (response.isSuccessful) {
                    String qrStr = (response.obj).toString();
                    String cord = qrStr.substring(1, qrStr.length() - 1);
                    initQr(cord);
                } else {
                    showToast(CodeTable.getInstance().getCodeDescription(response));
                }
                break;
        }
    }

    @OnClick(R.id.tv_btn)
    void OnClicked() {
        String money = etMoney.getText().toString().trim();
        if (money.equals("")) {
            showToast(getString(R.string.please_input_retrun_money));
        } else {
            llLayout.setVisibility(View.GONE);
            tvShowMoney.setVisibility(View.VISIBLE);
            if (money.startsWith(".")) {
                tvShowMoney.setText("￥0" + money);
            } else {
                tvShowMoney.setText("￥" + money);
            }
            showProgressCircle();
            microDrugRequest.setReturnMoney(money, SET_RETURN_MONEY_URL);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.toString().contains(".")) {
            if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                s = s.toString().subSequence(0,
                        s.toString().indexOf(".") + 3);
                etMoney.setText(s);
                etMoney.setSelection(s.length());
            }
        }
        if (s.toString().substring(0).equals(".")) {
            s = "0" + s;
            etMoney.setText(s);
            etMoney.setSelection(2);
        }

        if (s.toString().startsWith("0") && s.toString().trim().length() > 1) {
            if (!s.toString().substring(1, 2).equals(".")) {
                etMoney.setText(s.subSequence(0, 1));
                etMoney.setSelection(1);
                return;
            }
        }

        String text = etMoney.getText().toString();
        if (text.indexOf(".", text.indexOf(".") + 1) > 0) {
            text = text.substring(0, text.length() - 1);
            etMoney.setText(text);
            etMoney.setSelection(text.length());
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
        if (etMoney.getText().toString().trim().equals("")) {
            tvBtn.setBackground(ContextCompat.getDrawable(mActivity, R.color.auxiliary_text_bg));
            tvBtn.setClickable(false);
        } else {
            tvBtn.setBackground(ContextCompat.getDrawable(mActivity, R.color.colorPrimary));
            tvBtn.setClickable(true);
        }
    }
}
